// import dayjs from 'dayjs';

//弹出窗
export function showToast(title, icon) {
	uni.showToast({
		title: title,
		icon: icon ? icon : 'none',
	});
}

//时间戳转时间
export function formatDate(value) {
	let date = new Date(value);
	let y = date.getFullYear();
	let MM = date.getMonth() + 1;
	MM = MM < 10 ? ('0' + MM) : MM;
	let d = date.getDate();
	d = d < 10 ? ('0' + d) : d;
	let h = date.getHours();
	h = h < 10 ? ('0' + h) : h;
	let m = date.getMinutes();
	m = m < 10 ? ('0' + m) : m;
	let s = date.getSeconds();
	s = s < 10 ? ('0' + s) : s;
	return h + ':'+m;
}
//时间戳转时间
export function formatDateAll(value) {
	let date = new Date(value);
	let y = date.getFullYear();
	let MM = date.getMonth() + 1;
	MM = MM < 10 ? ('0' + MM) : MM;
	let d = date.getDate();
	d = d < 10 ? ('0' + d) : d;
	let h = date.getHours();
	h = h < 10 ? ('0' + h) : h;
	let m = date.getMinutes();
	m = m < 10 ? ('0' + m) : m;
	let s = date.getSeconds();
	s = s < 10 ? ('0' + s) : s;
	return y+'-'+MM+'-'+d+' '+h + ':'+m+':'+s;
}

//时间戳转时间
export function formatDateYearMonth(value) {
	let date = new Date(value);
	let y = date.getFullYear();
	let MM = date.getMonth() + 1;
	MM = MM < 10 ? ('0' + MM) : MM;
	let d = date.getDate();
	d = d < 10 ? ('0' + d) : d;
	let h = date.getHours();
	h = h < 10 ? ('0' + h) : h;
	let m = date.getMinutes();
	m = m < 10 ? ('0' + m) : m;
	let s = date.getSeconds();
	s = s < 10 ? ('0' + s) : s;
	return y+'-'+MM+'-'+d;
}
/**
 * 函数防抖 (只执行最后一次点击)
 */
 export const Debounce = (fn, t) => {
	let delay = t || 300;
	let timer;
	return function () {
	  let args = arguments;
	  if (timer) {
		clearTimeout(timer);
	  }
	  timer = setTimeout(() => {
		timer = null;
		fn.apply(this, args);
	  }, delay);
	}
  };


  /**
 * 获取当前用户位置
 */
  export const getWxLocation = () => {
	console.log('定位中...')
	return new Promise((resolve, reject) => {
	  let _locationChangeFn = (res) => {
		console.log('获取到定位', res)
		resolve(res)
		wx.offLocationChange(_locationChangeFn)
	  }
	  wx.startLocationUpdate({
		isHighAccuracy: true, // 开启地图精准定位
		type: 'gcj02',
		success: (res) => {
		  console.log(res);
		  wx.onLocationChange(_locationChangeFn)
  
		},
		fail: (err) => {
		  console.log('获取当前位置失败', err)
		  reject(err)
		}
	  })
	})
  }

  /**
 * 格式化时间，昨天，今天，刚刚
 */
  export function getTimeUtils(timestamp) {
	  const now = new Date();
    const date = new Date(timestamp);

    // 获取年、月、日
    const year = date.getFullYear();
    const month = date.getMonth() + 1; // 月份从0开始
    const day = date.getDate();
    const hours = date.getHours();
    const minutes = date.getMinutes();

    // 获取当前年、月、日
    const nowYear = now.getFullYear();
    const nowMonth = now.getMonth() + 1;
    const nowDay = now.getDate();

    // 计算时间差
    const diffDays = (now.getTime() - date.getTime()) / (1000 * 60 * 60 * 24);

    let formattedDate = '';
    if (diffDays < 1) {
      formattedDate = '';
    } else if (diffDays < 2) {
      formattedDate = '昨天';
    } else if (diffDays < 3) {
      formattedDate = '前天';
    } else {
      formattedDate = `${month}月${day}日`;
    }

    // 格式化时分
    const formattedTime = `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}`;

    return `${formattedDate}${formattedTime}`;
  }

   /**
 * 格式化时间，几周前，几小时前，几分钟前
 */
   export function getTimeUtils1(dateStr) {
	const now = new Date();
    const then = new Date(dateStr);
    const diff = now - then; // 时间差（毫秒）
    const seconds = Math.floor(diff / 1000);
    const minutes = Math.floor(seconds / 60);
    const hours = Math.floor(minutes / 60);
    const days = Math.floor(hours / 24);
    const weeks = Math.floor(days / 7);

    if (weeks > 0) {
      return `${weeks}周前`;
    } else if (days > 0) {
      return `${days}天前`;
    } else if (hours > 0) {
      return `${hours}小时前`;
    } else if (minutes > 0) {
      return `${minutes}分钟前`;
    } else {
      return '刚刚';
    }
   }

   /**
 * 格式化时间，月日，上午/下午—时间
 */
   export function formatTimestamp(timestamp) {
	const date = new Date(timestamp);
    const month = date.getMonth() + 1; // 月份从0开始，需要加1
    const day = date.getDate();
    const hours = date.getHours();
    const minutes = date.getMinutes().toString().padStart(2, '0');
    const ampm = hours >= 12 ? '下午' : '上午';
    const formattedHours = hours % 12 || 12; // 将24小时制转换为12小时制

    return `${month}月${day}日 ${ampm} ${formattedHours}:${minutes}`;
   }